# This is the extraction process DSL file.
#  Here the extraction can be done in both CSV as well as Fixed width.
class Extraction 
        
      # This method takes an array which will be the table names which you want to extract
      # In case all tables need to be extracted then option in :all
      def self.extract(table_hash)
          table_hash.each{|key,value|
              file_format = key[0]
              key = [key] if !key.respond_to? "join"
              key = key - [key.first]
              for single_key in key
                  required_table_list = []
                  table_list = get_all_table_list(single_key)
                  puts "Following tables are being extracted..."
                  if value.to_s != "all"
                      for single_value in value
                          required_table_list = required_table_list + get_required_columns(table_list,[single_value],"",single_key)  
                      end
                  else      
                      required_table_list = table_list
                  end    
                  if file_format.downcase == "yml"  
                      extract_tables_to_yml(required_table_list,single_key)
                  elsif file_format.downcase == "xml"
                      extract_tables_to_xml(required_table_list,single_key)
                  elsif file_format.downcase == "fixed_width"
                      extract_tables_to_fixed_width(required_table_list,single_key)
                  elsif file_format.downcase == "spread_sheet"
                      extract_tables_to_spread_sheet(required_table_list,single_key)    
                  elsif file_format.downcase == "csv"
                      extract_tables_to_csv(required_table_list,single_key)        
                  end    
              end    
          }        
      end	  
        
      # Selecting particular columns and extracting them.
      def self.filter_and_extract(options={})
          options.each{|key,value|
              all_connections = [key] if !key.respond_to? "join"
              file_format = all_connections.first
              all_connections = all_connections - [all_connections.first]
              all_table_names = []
              table_names = []
              for current_connection in all_connections  
                  all_tables = current_connection.tables
                  all_tables.each{|table|all_table_names << table.to_s}
                      value.each{|inner_key,inner_value|
                          all_required_tables = get_required_columns(all_table_names,inner_key,"",current_connection)
                          all_required_tables.uniq.each{|tab_name|
                              all_column_names = []
                              column_names = []
                              all_column_names = get_column_list(current_connection,tab_name)
                              column_names = get_required_columns(all_column_names,inner_value,"",current_connection)
                              if file_format.downcase == "xml"
                                  extract_column_to_xml(column_names.uniq,tab_name,current_connection)
                              elsif file_format.dowmcase == "yml"
                                  extract_column_to_yml(column_names.uniq,tab_name,current_connection)
                              elsif file_format.downcase == "csv"
                                  extract_column(column_names.uniq,tab_name,current_connection)
                              elsif file_format.downcase == "fixed_width"
                                  extract_column_to_fixed_width(column_names.uniq,tab_name,current_connection)
                              elsif file_format.downcase == "spread_sheet"      
                                  extract_column_to_spread_sheet(column_names.uniq,tab_name,current_connection)  
                              end      
                          }  
                      }
                end      
          }  
          puts "Selected columns were sucessfully extracted"
    end  

    # This for extracting all the tables from an array of connections
    def self.all_tables_from connection_array
        for connection in [connection_array]
            all_tables = connection.tables
            table_list = []
            if !all_tables.nil?
                  for table in all_tables
                        table_list << table
                  end
                  extract_tables(table_list,connection)
            else
                  puts "There are no tables under the given database"  
                end    
        end        
    end	
  
  
    # This method handles all the extract method calls.
    def self.method_missing(method_name,arg1)
        $tab_array = arg1
        extraction_in_process = Extraction.new
        return extraction_in_process
    end	

  
    # This method takes the connection as an argument.
    # Then it extracts the tables based on the global variables. 
    def from connection
        table_counter = 1
        tables_required = []
        all_tables = Extraction.get_all_table_list(connection)
        tables_required = get_required_columns(all_tables,$tab_array,"",connection)
        extract_tables(tables_required,connection)
    end		
  
  
    # This method gets all the table names from the database
    # This method is not for use directly but will be accessed by multiple other methods.
    def self.get_all_table_list(connection)
        all_tables_list = connection.tables
        table_list = []
        for table in all_tables_list
              table_list << table
        end	
        return table_list
    end	

end 	